/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.board;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.board.Conference;
import com.unis.service.attach.AttachLinkService;

/**
 * @author Alucard (email:357375084@qq.com)
 * @since 2013-03-14
 */
@Service("conferenceService")
public class conferenceServiceImpl implements conferenceService {

    private IDataSet4iBatis iDataSet4iBatis;
    private AttachLinkService attachlinkService;

    /****************************************
     * Insert a instace of conference
     ****************************************/
    public void insert(Conference conference) throws Exception {
        String fileid=conference.getFileid();
        conference.setConfid(TemplateUtil.genUUID());
        //将typeid的默认值替换
        if("&nbsp;&nbsp;".equals(conference.getTypeid())){
            conference.setTypeid("");
        }
        //如果附件不为空则循环插入附件
        if(!"".equals(fileid)&&fileid!=null){
            AttachLink al=new AttachLink();
            String[] files=fileid.split(",");
            for(String file: files){
                al.setFileid(file);
                al.setEntityid(conference.getConfid());
                al.setStatmentId("AttachLink.insert");
                attachlinkService.insert(al);
            }
        }
        iDataSet4iBatis.insert(conference);
    }

    /****************************************
     * Update a instace of conference
     ****************************************/
    public void update(Conference conference) throws Exception {
        iDataSet4iBatis.update(conference);
    }

    /****************************************
     * Delete a instace of conference
     ****************************************/
    public void delete(Conference conference) throws Exception {
        iDataSet4iBatis.delete(conference);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Conference conference) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Conference conference) throws Exception {
        String statementId = conference.getStatmentId();
        String mainId = conference.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> conferenceList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Conference model = new Conference();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            conferenceList.add(model);
        }
        iDataSet4iBatis.batchDelete(conferenceList);
    }

    /****************************************
     * Query instaces of conference
     ****************************************/
    public List<BaseModel> queryAsList(Conference conference) throws Exception {
        String queryListStatement = "";
        if (conference.isPaging()) {
            queryListStatement = conference.getStatmentId();
            conference.setStatmentId("Conference.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(conference);
            conference.setTotalCount(baseModel.getTotalCount());
            conference.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(conference);
    }

    /****************************************
     * Query a instaces of conference
     ****************************************/
    public BaseModel queryAsObject(Conference conference) throws Exception {
        return iDataSet4iBatis.queryAsObject(conference);
    }

    public List<BaseModel> queryReadyBoardRoom(Conference conference) throws Exception {
        conference.setPaging(false);
        conference.setStatmentId("Conference.queryReadyBoardRoom");
        return iDataSet4iBatis.queryAsList(conference);
    }    
    
    public List<BaseModel> queryBoardRoomState(Conference conference) throws Exception {
        conference.setPaging(false);
        conference.setStatmentId("Conference.queryBoardRoomState");
        return iDataSet4iBatis.queryAsList(conference);
    }
    
    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
    
    @Resource
    public void setAttachlinkService(AttachLinkService attachlinkService) {
        this.attachlinkService = attachlinkService;
    }   
}
